package com.allwees.support.payment.paypal.builder;

import com.paypal.orders.ApplicationContext;
import com.paypal.orders.OrderRequest;
import com.paypal.orders.PurchaseUnitRequest;
import com.allwees.bs.core.model.order.entity.OrderItemModel;
import com.allwees.bs.core.model.order.entity.OrderModel;
import com.allwees.core.common.constant.EPlatform;
import com.allwees.support.payment.model.PaymentInfo;
import com.allwees.support.payment.paypal.configuration.PaypalProperties;
import com.allwees.support.payment.paypal.constant.EPaypalPaymentIntent;

import java.util.List;

public class OrderRequestBuilder {

    public static OrderRequest buildEmpty() {
        return new OrderRequest();
    }

    public static OrderRequest build(PaymentInfo paymentInfo) {
        return new OrderRequest()
                .checkoutPaymentIntent(
                        EPaypalPaymentIntent.CAPTURE.name()
                )
                .applicationContext(
                        buildApplicationContext(paymentInfo.getOrderUuid(), paymentInfo.getPlatform(), paymentInfo.getPaypal())
                )
                .purchaseUnits(
                        buildPurchaseUnitRequests(paymentInfo.getOrder(), paymentInfo.getOrderItems())
                );
    }

    public static List<PurchaseUnitRequest> buildPurchaseUnitRequests(OrderModel order, List<? extends OrderItemModel> orderItems) {
        return PurchaseUnitRequestBuilder.build(order, orderItems);
    }

    public static ApplicationContext buildApplicationContext(String orderUuid, EPlatform platform, PaypalProperties paypal) {
        return new ApplicationContext()
//                .brandName("EXAMPLE INC")
                .landingPage("BILLING")
                .cancelUrl(urlParams(paypal.getCancelURL(), orderUuid, platform))
                .returnUrl(urlParams(paypal.getReturnURL(), orderUuid, platform))
                .userAction("CONTINUE")
//                .shippingPreference("SET_PROVIDED_ADDRESS");
                ;
    }

    private static String urlParams(String url, String orderUuid, EPlatform platform) {
        return url + "?orderUuid=" + orderUuid + "&platform=" + platform.name();
    }
}
